//
//  YsCloudRealmTool.m
//  YsCloudFileManageSDK
//
//  Created by 宋宏康 on 2020/2/26.
//

#import "YsCloudRealmTool.h"
#import "YsCloudFileRealmManager.h"
#import "YsCloudFileManageSDKRealm.h"
#import "YsCloudFileUploadReamModel.h"
#import "NSArray+QMUI.h"
#import "NSString+YscCloudFileTools.h"
#import <YYKit/YYKit.h>

@implementation YsCloudRealmTool

+ (NSArray <YsCloudFileUploadReamModel *>*)getUploadTaskWithAccountCode:(NSString *)accountCode
{
    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"accountCode == %@",accountCode];
    
      NSUInteger count = [YsCloudFileRealmManager queryObjsWithPredicate:predicate targetClass:YsCloudFileManageSDKRealm.class].count;
    
    if (count == 0) {
        return @[];
    }else{
        YsCloudFileManageSDKRealm *cloudFileManageSDKRealm = (YsCloudFileManageSDKRealm *) [YsCloudFileRealmManager queryObjsWithPredicate:predicate targetClass:YsCloudFileManageSDKRealm.class].lastObject;
        return (NSArray *)cloudFileManageSDKRealm.uploadReamModelArray;
    }
}

+ (NSArray <YsCloudFileDownloadRealmModel *>*)getDownloadTaskWithAccountCode:(NSString *)accountCode
{
    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"accountCode == %@",accountCode];
    NSArray *queryArray = [YsCloudFileRealmManager queryObjsWithPredicate:predicate targetClass:YsCloudFileManageSDKRealm.class];
    NSUInteger count = queryArray.count;
      if (count == 0) {
          return @[];
      }else{
          YsCloudFileManageSDKRealm *cloudFileManageSDKRealm = (YsCloudFileManageSDKRealm *) queryArray.lastObject;
          return (NSArray *)cloudFileManageSDKRealm.downloadReamModelArray;
      }
    return @[];
}

+ (NSArray <NSArray <YsCloudFileDownloadModel *> *>*)getDownloadTaskSortToFinishedWithDownloadArray:(NSArray <YsCloudFileDownloadModel *>*)downLoadArray
{
    NSMutableArray *array = [NSMutableArray array];
    NSArray *downloadFinishArray =  [downLoadArray qmui_filterWithBlock:^BOOL(YsCloudFileDownloadModel *item) {
        return item.downloadStatus == 4;
    }];
    
    NSArray *downloadNoFinishArray =  [downLoadArray qmui_filterWithBlock:^BOOL(YsCloudFileDownloadModel *item) {
          return item.downloadStatus != 4;
      }];
   
    if (downloadNoFinishArray.count >  0) {
        [array addObject:downloadNoFinishArray];
    }
    if (downloadFinishArray.count > 0) {
       [array addObject:downloadFinishArray];
    }
    return array;
}

+ (NSArray <NSArray <YsDocUploadModel *> *>*)getUploadTaskSortToFinishedWithDownloadArray:(NSArray <YsDocUploadModel *>*)upLoadArray
{
    NSMutableArray *array = [NSMutableArray array];
    NSArray *uploadFinishArray =  [upLoadArray qmui_filterWithBlock:^BOOL(YsDocUploadModel *item) {
        return item.uploadStatus == 4;
    }];
    
    NSArray *uploadNoFinishArray =  [upLoadArray qmui_filterWithBlock:^BOOL(YsDocUploadModel *item) {
          return item.uploadStatus != 4;
      }];
   
    if (uploadNoFinishArray.count >  0) {
        [array addObject:uploadNoFinishArray];
    }
    if (uploadFinishArray.count > 0) {
       [array addObject:uploadFinishArray];
    }
    return array;
}


+ (NSString *)getDownloadTaskWaitTimeWhenTaskBeginWithAccountCode:(NSString *)accountCode
{
    NSParameterAssert(accountCode);
    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"accountCode == %@",accountCode];
    NSArray *queryArray = [YsCloudFileRealmManager queryObjsWithPredicate:predicate keyPath:@"waitTaskTime" ascending:YES targetClass:YsCloudFileDownloadRealmModel.class];
    NSUInteger count = queryArray.count;
      if (count == 0) {
           return NSString.currentMillisecondTime;
       }else{
           YsCloudFileDownloadRealmModel *realmModel = queryArray.firstObject;
           return realmModel.waitTaskTime;
       }
}


+ (NSString *)getUploadTaskWaitTimeWhenTaskBeginWithAccountCode:(NSString *)accountCode
{
    NSParameterAssert(accountCode);
      NSPredicate *predicate = [NSPredicate predicateWithFormat:@"accountCode == %@",accountCode];
      NSArray *queryArray = [YsCloudFileRealmManager queryObjsWithPredicate:predicate keyPath:@"waitTaskTime" ascending:YES targetClass:YsCloudFileUploadReamModel.class];
      NSUInteger count = queryArray.count;
        if (count == 0) {
             return NSString.currentMillisecondTime;
         }else{
             YsCloudFileUploadReamModel *realmModel = queryArray.firstObject;
             return realmModel.waitTaskTime;
         }
}

@end
